#include "mainwindow.h"
#include <QLabel>
#include "utils.h"
#include <QTreeWidget>
#include <QString>
#include <iostream>
#include <arpa/inet.h>
#include "dashbutton.h"



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{



//form  for input peer or route 's info

//        QLineEdit *nameLineEdit  = new QLineEdit();
//        QLineEdit *emailLineEdit  = new QLineEdit();

//        QFormLayout *formLayout = new QFormLayout(this);
//        formLayout->addRow(tr("&Name:"), nameLineEdit);
//        formLayout->addRow(tr("&Email:"), emailLineEdit);

//        QWidget *win = new QWidget();
//        win->setLayout(formLayout);

//
        resize(1000,600);
        utils ut;
// info tree
        QTreeWidget *networkTree = new QTreeWidget;

        //set view
        networkTree->setHeaderLabels({"Interface", "", "",""});
        networkTree->setColumnWidth(2,500);
        for(int num=0; num <= ut.numOfIF-1; num++){
            QTreeWidgetItem *deviceItem = new QTreeWidgetItem(networkTree);

            //device name
            deviceItem->setText(0, ut.deviceName[num]);

//            DashButton *dbtn = new DashButton("ddddd");

//            networkTree->setItemDelegateForColumn(1,deviceItem,new ButtonDelegate);
            networkTree->setItemWidget(deviceItem,1,new DashButton("Edit"));
            networkTree->setItemWidget(deviceItem,2,new DashButton("Delete"));
            //device or interface info

                    wg_device *dev = ut.getDeviceBYName(ut.deviceName[num]);                    
                    QTreeWidgetItem *interfaceItem0 = new QTreeWidgetItem(deviceItem);
                    if(dev->public_key[0]=='\0') continue;
                    interfaceItem0->setText(1, "public key");
                    interfaceItem0->setText(2, QString::fromStdString( ut.wgKey2Base64(dev->public_key)));

                    QTreeWidgetItem *interfaceItem1 = new QTreeWidgetItem(deviceItem);
                    interfaceItem1->setText(1, "private key");
                    interfaceItem1->setText(2, "hidden");
                    QTreeWidgetItem *interfaceItem2 = new QTreeWidgetItem(deviceItem);
                    interfaceItem2->setText(1, "listening port");
                    interfaceItem2->setText(2, QString::number(dev->listen_port));
                    //info of peers
                    QTreeWidgetItem *peers = new QTreeWidgetItem(deviceItem);

                    peers->setText(0, "peers");
                    networkTree->setItemWidget(peers,1,new DashButton("Add  Peer"));
                    // display info of each single peer
                    for(wg_peer *peer= dev->first_peer;peer;peer=peer->next_peer){
                        QTreeWidgetItem *peerItem0 = new QTreeWidgetItem(peers);
                        networkTree->setItemWidget(peerItem0,3,new DashButton("Delete Peer"));
                        peerItem0->setText(1,"peer");
                        peerItem0->setText(2, QString::fromStdString(ut.wgKey2Base64(peer->public_key) ));

                        QTreeWidgetItem *ipeerItem1 = new QTreeWidgetItem(peers);
                        ipeerItem1->setText(1,"endpoint");

                        char ip_str[INET_ADDRSTRLEN];
                        int port = ntohs(peer->endpoint.addr4.sin_port);
                        inet_ntop(AF_INET, &(peer->endpoint.addr4.sin_addr), ip_str, INET_ADDRSTRLEN);

                        std::string addrString = std::string(ip_str) + ":" + std::to_string(port);
                        ipeerItem1->setText(2, QString::fromStdString(addrString));


                        QTreeWidgetItem *item2 = new QTreeWidgetItem(peers);
                        item2->setText(1,"allowed ips");

                        std::string ip_cidr;
                        for(wg_allowedip *allowedip=peer->first_allowedip;allowedip;allowedip=allowedip->next_allowedip){

                            char allowediparr[INET_ADDRSTRLEN];
                            inet_ntop(AF_INET, &allowedip->ip4, allowediparr, INET_ADDRSTRLEN);
                            std::string allowedipstr(allowediparr);
                            ip_cidr += allowedipstr+"/"+std::to_string(static_cast<int>(allowedip->cidr))+";";
                             std::cout <<"tes" << ip_cidr << std::endl;
                        }

                        item2->setText(2,QString::fromStdString(ip_cidr));
                    }
        }

        setCentralWidget(networkTree);

}

MainWindow::~MainWindow()
{



}

